#include <stdio.h>
#include <stdlib.h>

#include "libs/strtools.h"
#include "libs/print.h"
#include "libs/compare.h"
#include "libs/option.h"


//fonction main, ne pas toucher aux parametres
//argc: nombre de parametres passes au programme
//argv: tableau contenant les parametres sous forme de chaine
int main(int argc, char** argv){

	char** compareErrors;
	int count = 0;
	int nb_ligne1=0, nb_ligne2=0;
	
	if(!argv[argc-2]){
	  printf("diff: missing operand after `diff'\ndiff: Try `diff --help' for more information.\n");
	  return 1;
	}else if(!my_strcmp(argv[1],"--help")){
	  printFileContent("help.txt\0");
	  return 1;
	}else if(!argv[argc-1]){
	  printf("diff: missing operand after `diff'\ndiff: Try `diff --help' for more information.\n");
	  return 1;
	}
	
	char** file1 = getFileContent(argv[argc-2], &nb_ligne1);
	char** file2 = getFileContent(argv[argc-1], &nb_ligne2);
	int line = 0;
	int line2 = 0;
	int param = 0;
	while(line<nb_ligne1){
		param = 0;
		while(param<argc-1){
		  if(argv[param][0]=='-'){
			switch(argv[param][1]){
				case 'i':
					file1[line] = ignoreCase(file1[line]);
					break;
				case 'E':
					file1[line] = ignoreTab(file1[line]);
					break;
				case 't':
					file1[line] = expandTab(file1[line]);
					break;
				case 'b':
					file1[line] = ignoreSpaceChange(file1[line]);
					break;
				case 'w':
					file1[line] = ignoreSpace(file1[line]);
					break;
				case 'B':
					file1[line] = ignoreBlankLines(file1[line]);
					break;
				  default:
				break;
			}
		  }
		  ++param;
		}
		line++;
	}
	
	while(line2<nb_ligne2){
		param = 0;
		while(param<argc-1){
		  if(argv[param][0]=='-'){
			switch(argv[param][1]){
				case 'i':
					file2[line2] = ignoreCase(file2[line2]);
					break;
				case 'E':
					file2[line2] = ignoreTab(file2[line2]);
					break;
				case 't':
					file2[line2] = expandTab(file2[line2]);
					break;
				case 'b':
					file2[line2] = ignoreSpaceChange(file2[line2]);
					break;
				case 'w':
					file2[line2] = ignoreSpace(file2[line2]);
					break;
				case 'B':
					file2[line2] = ignoreBlankLines(file2[line2]);
					break;
				  default:
				break;
			}
		  }
		  ++param;
		}
		line2++;
	}

	compareErrors = compare(&count, file1, file2, nb_ligne1, nb_ligne2);
	printDiff(compareErrors, count, file1, file2);

	free(file1);
	free(file2);
	free(compareErrors);
	return 0;
}
